using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;


public class XMLGems : MonoBehaviour
{
    XmlReader reader;
    XmlDocument doc = new XmlDocument();
    XmlNodeList n;
    XmlNode c;
    Gems gem = new Gems();
    int Soom;
    private List<int> freq = null;
	private List<int> Freq = null;
    public List<int> randomizeList { get { if (freq == null) fill(); return freq; } }
	public List<int> ReRandomizeList { get { if (Freq == null) fill(6); return Freq; } }
    private bool m_Initialized = false;
	private static bool loaded=false;
    public void getAllAtribsInGem()
    {
		doc.LoadXml((Resources.Load("gems") as TextAsset).text);
        n = doc.GetElementsByTagName("Gem");
        foreach (XmlNode c in n)
        {
            gem.id.Add(System.Convert.ToInt32(c.Attributes.GetNamedItem("id").Value));
            gem.Name.Add(c.Attributes.GetNamedItem("name").Value);
            gem.SpriteName.Add(c.Attributes.GetNamedItem("Spritename").Value);
            gem.Score.Add(System.Convert.ToInt32(c.Attributes.GetNamedItem("Score").Value));
            gem.movable.Add(System.Convert.ToBoolean(c.Attributes.GetNamedItem("move").Value));
            gem.Frequncy.Add(System.Convert.ToInt32(c.Attributes.GetNamedItem("Frequency").Value));
			gem.Destructable.Add (System.Convert.ToBoolean(c.Attributes.GetNamedItem("Destructable").Value));
			gem.Generable.Add (System.Convert.ToBoolean(c.Attributes.GetNamedItem("Generable").Value));
						gem.r.Add (System.Convert.ToSingle(c.Attributes.GetNamedItem ("red").Value));
						//Debug.Log (c.Attributes.GetNamedItem("red").Value);
						gem.g.Add (System.Convert.ToSingle(c.Attributes.GetNamedItem("green").Value));
						gem.b.Add (System.Convert.ToSingle(c.Attributes.GetNamedItem("blue").Value));
        }
        m_Initialized = true;
    }

	public Color GetColor(int id)
	{
				Color color = new Color ();
		if(!m_Initialized){
			getAllAtribsInGem();}
		//		Debug.Log(id);
		//		Debug.Log(gem.id.Count);
		if(id <= 0)
		{
			color = new Color ((float)gem.r[id],(float)gem.g[id],(float)gem.b[id]);
			return color;
		}
		else 
			if(id > gem.id.Count)
		{
			id = gem.Score.Count;
			color = new Color ((float)gem.r[id - 1],(float)gem.g[id - 1],(float)gem.b[id - 1]);
						return color;
		}
		color = new Color ((float)gem.r[id - 1],(float)gem.g[id - 1],(float)gem.b[id - 1]);
				return color;

	}

    public int GetScore(int id)
    {   
		if(!m_Initialized){
		getAllAtribsInGem();}
//		Debug.Log(id);
//		Debug.Log(gem.id.Count);
		if(id <= 0)
		{
			return System.Convert.ToInt32(gem.Score[id+1]);
		}
		else 
			if(id > gem.id.Count)
		{
			id = gem.Score.Count;
			return System.Convert.ToInt32(gem.Score[id-1]);
		}
		return System.Convert.ToInt32(gem.Score[id-1]);
    }
    public int GetFrequency(int id)
    {
		 if(id == 0)
		{
			return System.Convert.ToInt32(gem.Frequncy[id+1]);
		}
		else if(id > gem.id.Count)
		{
			id = gem.id.Count;
			return System.Convert.ToInt32(gem.Frequncy[id-1]);
		}
		Debug.Log(gem.Frequncy[id-1]);
		return System.Convert.ToInt32(gem.Frequncy[id-1]);
    }
    public bool IsMovable(int id)
    {
	 if(id == 0)
		{ return true;
			//return System.Convert.ToBoolean(gem.movable[id+1]);
		}
		else if(id > gem.id.Count)
		{
			id = gem.movable.Count;
			return System.Convert.ToBoolean(gem.movable[id-1]);
		}
		return System.Convert.ToBoolean(gem.movable[id-1]);
    }
    public string spriteName(int id)
    {
		if(!m_Initialized){
		getAllAtribsInGem();}
	 if(id == 0)
		{
			return System.Convert.ToString(gem.SpriteName[id+1]);
		}
		else 
			if(id > gem.id.Count)
		{
			return System.Convert.ToString(gem.SpriteName[id-1]);
			Debug.Log(id);
		}
		return System.Convert.ToString(gem.SpriteName[id-1]);
    }
	public bool isDestruct(int id)
	{
		if(!m_Initialized){
			getAllAtribsInGem();}
		if(id == 0)
		{
			return System.Convert.ToBoolean(gem.Destructable[id+1]);
		}
		else 
			if(id > gem.id.Count)
		{
			return System.Convert.ToBoolean(gem.Destructable[id-1]);
			Debug.Log(id);
		}
				return System.Convert.ToBoolean (gem.Destructable [id - 1]);
	}

	public bool IsGenerate(int id)
	{
		if(!m_Initialized){
			getAllAtribsInGem();}
		if(id == 0)
		{
			return System.Convert.ToBoolean(gem.Generable[id + 1]);
		}
		else 
			if(id > gem.id.Count)
		{
			return System.Convert.ToBoolean(gem.Generable[id - 1]);
			Debug.Log(id);
		}
		return System.Convert.ToBoolean (gem.Generable[id - 1]);
	}

	public List<int> fill(int with)
	{
		if (!(IsGenerate (with)))
			//Debug.Log (IsGenerate(6));
			gem.Frequncy [with - 1] = 0;
		//Debug.Log (gem.Frequncy[with - 1]);
		Freq = new List<int>();
		for (int i = 1; i < gem.Frequncy.Count + 1; i++)
		{
		//	Debug.Log (gem.Frequncy[i - 1]);
			for (int j = 0; j < System.Convert.ToInt32(gem.Frequncy[i - 1]); j++)
				Freq.Add(i);
		}
				return Freq;
	}

    public List<int> fill()
    {
        //if (!m_Initialized) getAllAtribsInGem();
        freq = new List<int>();
        for (int i = 1; i < gem.Frequncy.Count + 1; i++)
        {
            for (int j = 0; j < System.Convert.ToInt32(gem.Frequncy[i - 1]); j++)
                freq.Add(i);
        }
        return freq;
    }
    public int GetGemByID()
    {
        int r = Random.Range(0, randomizeList.Count);
        return randomizeList[r];
    }
	public int AddGemByID()
	{
		int r = Random.Range(0, ReRandomizeList.Count);
				return ReRandomizeList [r];
	}
}
